<?php

/*
 * This class is used for as Model for Product table 
 */

class Model_Location extends Model_Base {

    protected $_name = 'tbl_location';
    protected $_primaryKey = 'id';
    
    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchNearestLocation($lat, $lon) {
		
		if ($lat == '') {
			return ;
		}
		
		if ($lon == '') {
			return ;
		}

        $select = $this->select()
                ->from(array('location' => 'tbl_location'), Array('id','user_id','lat','lon','distance' => '( 3959 * ACOS( COS( RADIANS('.$lat.') ) * COS( RADIANS( lat ) ) * COS( RADIANS( lon ) - RADIANS('.$lon.') ) + SIN( RADIANS('.$lat.') ) * SIN( RADIANS( lat ) ) ) )'));
		$select->setIntegrityCheck(false)
                ->join(array('user' => 'tbl_user'), "location.user_id =  user.user_id");
				
				
        $select->order("distance ASC");
		$select->limit(1, 0);
		
        $data = parent::fetchRow($select);
		return $data;
        
    }

    
    

}
